package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO 1
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 创建ShoppingCart对象，并从DTO复制属性
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 设置当前用户ID
        shoppingCart.setUserId(BaseContext.getCurrentId());

        // 查询购物车中是否已存在该商品
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart); // 查询结果只有两种情况：1、无数据，2、有一条数据

        // 如果商品已存在，则增加数量
        if (list != null && list.size() > 0) {
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            // 更新商品数量
            shoppingCartMapper.updateById(cart);
        } else {
            // 商品不存在，执行插入操作
            // 判断是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                // 如果dishId不为空，则为菜品
                Dish dish = dishMapper.getById(dishId);

                // 设置商品名称、图片和价格
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                // 如果dishId为空，则为套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());

                // 设置套餐名称、图片和价格
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            // 设置商品数量和创建时间
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            // 插入新商品到购物车
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     *
     * @return 1
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        // 构建ShoppingCart对象，设置当前用户ID
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        // 查询当前用户购物车中的商品列表
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        // 返回购物车商品列表
        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.delAllByUserId(BaseContext.getCurrentId());
    }

    /**
     * 减少商品数量
     *
     * @param shoppingCartDTO 1
     */
    @Override
    public void reduceNums(ShoppingCartDTO shoppingCartDTO) {
        // 创建ShoppingCart对象，并从DTO复制属性
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 设置当前用户ID
        shoppingCart.setUserId(BaseContext.getCurrentId());

        // 查询购物车中是否已存在该商品
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart); // 查询结果只有两种情况：1、无数据，2、有一条数据

        // 如果商品已存在，则修改数量
        if (list != null && list.size() > 0) {
           shoppingCart = list.get(0);
            if (shoppingCart.getNumber() == 1)
                shoppingCartMapper.delById(shoppingCart.getId());
            else {
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                shoppingCartMapper.updateById(shoppingCart);
            }
        }
    }
}
